package com.cungmua.app.task;

import android.content.Context;

import com.cungmua.app.handler.BaseAuthenticatedRequestHandler;
import com.cungmua.app.handler.CreateShippingAddressHandler;
import com.cungmua.app.model.request.CreateShippingAddressRequest;
import com.cungmua.app.model.response.CreateShippingAddressResponse;
import com.cungmua.app.net.CungMuaAPI;

import retrofit.RetrofitError;

/**
 * Created by kelvin.nguyen on 6/25/14.
 */
public class CreateShippingAddressTask
        extends AuthenticatedTask<CreateShippingAddressRequest, Void, CreateShippingAddressResponse> {

    private final CreateShippingAddressHandler handler;
    private final Context context;

    public CreateShippingAddressTask(Context context, CreateShippingAddressHandler handler) {
        this.context = context;
        this.handler = handler;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected CreateShippingAddressResponse doInBackground() throws RetrofitError {
        CreateShippingAddressRequest request = params[0];

        return CungMuaAPI.getService().createShippingAddress(request.toFieldMap());
    }

    @Override
    protected BaseAuthenticatedRequestHandler getHandler() {
        return handler;
    }

    @Override
    protected void onSuccess(CreateShippingAddressResponse createShippingAddressResponse) {
        handler.onShippingAddressCreated(createShippingAddressResponse);
    }
}

